package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Employee;
import com.itheima.common.R;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.rmi.Remote;
import java.time.LocalDateTime;


@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee employee1 = employeeService.getOne(wrapper);
        System.out.println(employee1);
        if (employee1 == null) {
            return R.error("用户名错误或不存在!");
        }

        if (!DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()).equals(employee1.getPassword())) {
            return R.error("密码错误!");
        }

        if (employee1.getStatus() == 0) {
            return R.error("员工已禁用");
        }
        request.getSession().setAttribute("employee", employee1.getId());
        return R.success(employee1);

    }

    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success(null);
    }

    //新增员工
    @PostMapping
    public R<String> add(@RequestBody Employee employee, HttpServletRequest request) {
        //设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean flag = employeeService.save(employee);
        if (flag){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    //分页查询+条件查询
    @GetMapping("/page")
    //参数以k:v格式转发，不用加注解
    public R<IPage> SelectPage(Integer page ,Integer pageSize,String name){
        Page<Employee> employeePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(name != null,Employee::getName,name);
        Page<Employee> ipage = employeeService.page(employeePage, wrapper);

        return R.success(ipage);
    }

    /**
     * 修改员工的状态
     * 修改回显的数据进行提交修改
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee,HttpServletRequest request){
        System.out.println(employee);
        Long UserId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(UserId);
        employee.setUpdateTime(LocalDateTime.now());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //根据id查询编辑用户的信息回显到页面
    @GetMapping("/{id}")
    public R<Employee> updateId(@PathVariable Long id){
        Employee emp = employeeService.getById(id);
        return R.success(emp);
    }




}
